package com.hp.snap.gr.action.impl;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.model.jaxb.SCPFromRemoteActionType;
import com.hp.snap.gr.utils.SSHTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class SCPFromRemoteActionHandler extends AbstractActionHandler {

    private static Logger logger = LoggerFactory.getLogger(SCPFromRemoteActionHandler.class);

    @Override
    public void execute(ActionType action,
                        Map<String, Object> actionPlanInputs,
                        Map<String, Object> actionPlanOutputs) throws ActionException {

        logger.debug("invoke the SCPFromRemoteActionHandler");
        SCPFromRemoteActionType actionType = (SCPFromRemoteActionType) action;

        List<String> remoteFiles = actionType.getRemote();
        String localPath = actionType.getLocal();
        try {
            SSHTools ssh = new SSHTools(null, null);
            for (String remoteFile : remoteFiles) {
                ssh.copyFromRemote(localPath, actionType.getRemoteHost(), remoteFile);
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ActionException(e);
        }
    }

}
